<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" 
	xmlns:ui="http://java.sun.com/jsf/facelets" 
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui" 
	xmlns:hf="http://java.sun.com/jsf/composite/tags"
	xmlns:c="http://java.sun.com/jsp/jstl/core"
	xmlns:cc="http://java.sun.com/jsf/composite">

	<cc:interface componentType="searchField">
		<cc:attribute name="field" required="true" />
		<cc:attribute name="backingBean" required="false" />
		<cc:attribute name="disabled" default="false" />
		<cc:attribute name="size" default="20" />
		<cc:attribute name="rangeSearch" default="true" />
		<cc:attribute name="padding" default="5px" />
		<cc:attribute name="time" default="false" />
		<cc:attribute name="childField"  required="false" />
		<cc:attribute name="listBean" required="false" />
		<cc:attribute name="popup" default="false" />
		<cc:attribute name="popupWidget" default="dlg" />
	</cc:interface>
	
	<cc:implementation>

		<ui:fragment rendered="#{cc.isText(cc.attrs.field)}">
			<h:panelGrid columns="2">
				<p:outputLabel for="#{cc.attrs.field}_text" value="#{cc.attrs.label}" />
				<p:inputText id="#{cc.attrs.field}_text" label="#{cc.attrs.label}" value="#{cc.filters[cc.attrs.field]}" size="#{cc.attrs.size}" disabled="#{cc.attrs.disabled}" />
			</h:panelGrid>
		</ui:fragment>
		
		<!--  If the value binding is a boolean, display a drop down with yes/no choices. -->
		<ui:fragment rendered="#{cc.isBoolean(cc.attrs.field)}">
			<h:panelGrid columns="2">
				<p:outputLabel for="#{cc.attrs.field}_boolean" value="#{cc.attrs.label}" />
				<p:selectOneMenu id="#{cc.attrs.field}_boolean" value="#{cc.filters[cc.attrs.field]}" label="#{cc.attrs.label}" converter="javax.faces.Boolean" disabled="#{cc.attrs.disabled}">
					<f:selectItem itemValue="#{null}" itemLabel="" />
					<f:selectItem itemValue="#{true}" itemLabel="#{messages['commons.yes']}" />
					<f:selectItem itemValue="#{false}" itemLabel="#{messages['commons.no']}" />
				</p:selectOneMenu>
			</h:panelGrid>
		</ui:fragment>
		
		<!--  If the value binding is a date, display a p:calendar field. -->
		<ui:fragment rendered="#{cc.isDate(cc.attrs.field)}">
			<ui:fragment rendered="#{not cc.attrs.rangeSearch}">
				<h:panelGrid columns="2">
					<p:outputLabel for="#{cc.attrs.field}_date" value="#{cc.attrs.label}" />
					<p:calendar id="#{cc.attrs.field}_date" value="#{cc.filters[cc.attrs.field]}" label="#{cc.attrs.label}" pattern="#{cc.datePattern}" disabled="#{cc.attrs.disabled}" />
				</h:panelGrid>
			</ui:fragment>
			<ui:fragment rendered="#{cc.attrs.rangeSearch}">
				<h:panelGrid columns="2">
					<p:outputLabel for="fromRange_#{cc.attrs.field}_date" value="#{cc.attrs.label}" />
					<h:panelGrid columns="4">
						<p:calendar id="fromRange_#{cc.attrs.field}_date" value="#{cc.filters[cc.fromRangeSearchFilterName]}" label="#{cc.attrs.label}" pattern="#{cc.datePattern}" disabled="#{cc.attrs.disabled}" />
						<p:watermark for="fromRange_#{cc.attrs.field}_date" value="#{messages['commons.dateFrom']}" />  
						
						<p:calendar id="toRange_#{cc.attrs.field}_date" value="#{cc.filters[cc.toRangeSearchFilterName]}" label="#{cc.attrs.label}" pattern="#{cc.datePattern}" disabled="#{cc.attrs.disabled}" />
						<p:watermark for="toRange_#{cc.attrs.field}_date" value="#{messages['commons.dateTill']}" />  
					</h:panelGrid>
				</h:panelGrid>
			</ui:fragment>
		</ui:fragment>
		
		<!--  If the value binding is an enum, display a drop down. -->
		<ui:fragment rendered="#{cc.isEnum(cc.attrs.field)}">
			<h:panelGrid columns="2">
				<p:outputLabel for="#{cc.attrs.field}_enum" value="#{cc.attrs.label}" />
				<p:selectOneMenu id="#{cc.attrs.field}_enum" value="#{cc.filters[cc.attrs.field]}" label="#{cc.attrs.label}" disabled="#{cc.attrs.disabled}">
					<f:selectItem itemLabel=""  />
					<f:selectItems value="#{cc.getEnumConstants(cc.attrs.field)}" var="enumElement" itemValue="#{enumElement}" itemLabel="#{messages[enumElement.label]}"/>
					<f:converter converterId="enumConverter" />
				</p:selectOneMenu>
			</h:panelGrid>
		</ui:fragment>
		
		<!--  If the value binding is a number display number field. -->
		<ui:fragment rendered="#{cc.isInteger(cc.attrs.field)}">
			<hf:numberField converterId="javax.faces.Integer" />
		</ui:fragment>
		<ui:fragment rendered="#{cc.isLong(cc.attrs.field)}">
			<hf:numberField converterId="javax.faces.Long" />
		</ui:fragment>
		<ui:fragment rendered="#{cc.isByte(cc.attrs.field)}">
			<hf:numberField converterId="javax.faces.Byte" />
		</ui:fragment>
		<ui:fragment rendered="#{cc.isShort(cc.attrs.field)}">
			<hf:numberField converterId="javax.faces.Short" />
		</ui:fragment>
		<ui:fragment rendered="#{cc.isDouble(cc.attrs.field)}">
			<hf:numberField converterId="javax.faces.Double" />
		</ui:fragment>
		<ui:fragment rendered="#{cc.isFloat(cc.attrs.field)}">
			<hf:numberField converterId="javax.faces.Float" />
		</ui:fragment>
		<ui:fragment rendered="#{cc.isBigDecimal(cc.attrs.field)}">
			<hf:numberField converterId="javax.faces.BigDecimal" />
		</ui:fragment>
		
		<ui:fragment rendered="#{cc.isEntity(cc.attrs.field)}">
			<!-- Search with entity popup -->
		    <ui:fragment rendered="#{cc.attrs.popup}">
				<h:panelGrid columns="2">
					<p:outputLabel for="#{cc.attrs.field}_child" value="#{cc.attrs.label}" />
					<h:panelGrid id="#{cc.attrs.field}" columns="3">
						<p:inputText id="#{cc.attrs.field}_child" value="#{cc.filters[cc.attrs.field][cc.attrs.childField]}" required="#{cc.attrs.required}" size="#{cc.attrs.size}" disabled="true" />
						<p:commandButton id="selectLink" onclick="#{cc.attrs.popupWidget}.show()" icon="ui-icon-search"/>
						<p:tooltip for="selectLink" value="#{messages['commons.search']}" />
					</h:panelGrid>
				</h:panelGrid>
			</ui:fragment>

			<!-- Search with entity list in select box -->
		    <ui:fragment rendered="#{not cc.attrs.popup and not empty cc.attrs.listBean}">
	    		<h:panelGrid columns="2">
					<p:outputLabel for="_#{cc.attrs.id}_level" value="#{cc.attrs.label}" />
					<p:selectOneMenu id="_#{cc.attrs.id}_level" value="#{cc.filters[cc.attrs.field]}">
						<f:selectItem itemLabel="" itemValue="" />
						<f:selectItems value="#{cc.attrs.listBean.listAll()}" var="elem" itemLabel="#{elem[cc.attrs.childField]}" itemValue="#{elem}" />
						<f:converter binding="#{entityConverter}" />
					</p:selectOneMenu>
				</h:panelGrid>
	    	</ui:fragment>
		</ui:fragment>
		
	</cc:implementation>

</html>
